package o;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.basement.codec.annotation.NamedField;
import com.huawei.appgallery.basement.codec.annotation.Transient;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class bku {
    @NonNull
    public String ॱ(Object obj) {
        return ॱ(obj, new JSONObject()).toString();
    }

    @NonNull
    private JSONObject ॱ(@Nullable Object obj, @NonNull JSONObject jSONObject) {
        if (obj != null) {
            Class cls = obj.getClass();
            while (cls != null && cls != Object.class) {
                for (Field ˎ : cls.getDeclaredFields()) {
                    ˎ(obj, ˎ, jSONObject);
                }
                cls = cls.getSuperclass();
            }
        }
        return jSONObject;
    }

    private void ˎ(@NonNull Object obj, @NonNull Field field, @NonNull JSONObject jSONObject) {
        if (!Modifier.isTransient(field.getModifiers()) && field.getAnnotation(Transient.class) == null) {
            boolean isAccessible = field.isAccessible();
            field.setAccessible(true);
            String ˎ = ˎ(field);
            try {
                Object ˊ = ˊ(field.get(obj));
                if (ˊ != null) {
                    jSONObject.put(ˎ, ˊ);
                }
            } catch (IllegalAccessException e) {
                bkp.ॱ.ˊ("JSONCodec", "Unreachable, IllegalAccessException when encoding, field name: " + ˎ);
            } catch (JSONException e2) {
                bkp.ॱ.ˊ("JSONCodec", "Unreachable, JSONException when encoding, field name: " + ˎ);
            }
            field.setAccessible(isAccessible);
        }
    }

    private Object ˊ(@Nullable Object obj) throws JSONException {
        if (obj == null) {
            return null;
        }
        if (obj instanceof bkm) {
            return ॱ(obj, new JSONObject());
        }
        if (!(obj instanceof List) && !(obj instanceof Set)) {
            return obj;
        }
        JSONArray jSONArray = new JSONArray();
        for (Object ˊ : (Collection) obj) {
            jSONArray.put(ˊ(ˊ));
        }
        return jSONArray;
    }

    @Nullable
    public <T> T ˊ(@NonNull Class<T> cls, @NonNull String str) {
        try {
            return ˋ(cls, new JSONObject(str));
        } catch (JSONException e) {
            bkp.ॱ.ˊ("JSONCodec", "JSONException when creating JSONObject from a string.");
            return null;
        }
    }

    @Nullable
    public <T> T ˋ(@NonNull Class<T> cls, @NonNull JSONObject jSONObject) {
        T t = null;
        try {
            t = cls.newInstance();
            while (cls != null && cls != Object.class) {
                for (Field ˋ : cls.getDeclaredFields()) {
                    ˋ(t, ˋ, jSONObject);
                }
                cls = cls.getSuperclass();
            }
        } catch (IllegalAccessException e) {
            bkp.ॱ.ˊ("JSONCodec", "Unreachable, IllegalAccessException when decoding, class: " + cls);
        } catch (InstantiationException e2) {
            bkp.ॱ.ˊ("JSONCodec", "Unreachable, InstantiationException when decoding, class: " + cls);
        }
        return t;
    }

    private void ˋ(@NonNull Object obj, @NonNull Field field, @NonNull JSONObject jSONObject) {
        if (!Modifier.isTransient(field.getModifiers()) && field.getAnnotation(Transient.class) == null) {
            String ˎ = ˎ(field);
            try {
                Object ˏ = ˏ(field, ˎ, jSONObject);
                if (ˏ != null) {
                    boolean isAccessible = field.isAccessible();
                    field.setAccessible(true);
                    field.set(obj, ˏ);
                    field.setAccessible(isAccessible);
                }
            } catch (JSONException e) {
                bkp.ॱ.ˊ("JSONCodec", "Unreachable, JSONException when decoding, field name: " + ˎ);
            } catch (IllegalAccessException e2) {
                bkp.ॱ.ˊ("JSONCodec", "Unreachable, IllegalAccessException when decoding, field name: " + ˎ);
            }
        }
    }

    @Nullable
    private Object ˏ(@NonNull Field field, @NonNull String str, @NonNull JSONObject jSONObject) throws JSONException {
        Class type = field.getType();
        Object obj = jSONObject.get(str);
        if (bkm.class.isAssignableFrom(type)) {
            return obj instanceof JSONObject ? ˋ(type, (JSONObject) obj) : obj;
        } else {
            if (List.class.isAssignableFrom(type)) {
                if (!(obj instanceof JSONArray)) {
                    return obj;
                }
                return ˎ(new ArrayList(), (Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0], (JSONArray) obj);
            } else if (!Set.class.isAssignableFrom(type)) {
                return ˎ(type, jSONObject, str);
            } else {
                if (!(obj instanceof JSONArray)) {
                    return obj;
                }
                return ˎ(new HashSet(), (Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0], (JSONArray) obj);
            }
        }
    }

    @NonNull
    private Object ˎ(@NonNull Collection<Object> collection, @NonNull Class<?> cls, @NonNull JSONArray jSONArray) throws JSONException {
        for (int i = 0; i < jSONArray.length(); i++) {
            Object obj = jSONArray.get(i);
            if (obj instanceof JSONObject) {
                collection.add(ˋ(cls, (JSONObject) obj));
            } else {
                collection.add(ˊ(cls, jSONArray, i));
            }
        }
        return collection;
    }

    @NonNull
    private Object ˎ(@NonNull Class<?> cls, @NonNull JSONObject jSONObject, @NonNull String str) throws JSONException {
        if (cls == String.class) {
            return jSONObject.getString(str);
        }
        if (cls == Boolean.class || cls == Boolean.TYPE) {
            return Boolean.valueOf(jSONObject.getBoolean(str));
        }
        if (cls == Double.class || cls == Double.TYPE) {
            return Double.valueOf(jSONObject.getDouble(str));
        }
        if (cls == Float.class || cls == Float.TYPE) {
            return Float.valueOf((float) jSONObject.getDouble(str));
        }
        if (cls == Integer.class || cls == Integer.TYPE) {
            return Integer.valueOf(jSONObject.getInt(str));
        }
        if (cls == Short.class || cls == Short.TYPE) {
            return Short.valueOf((short) jSONObject.getInt(str));
        }
        if (cls == Long.class || cls == Long.TYPE) {
            return Long.valueOf(jSONObject.getLong(str));
        }
        return jSONObject.get(str);
    }

    @NonNull
    private Object ˊ(@NonNull Class<?> cls, @NonNull JSONArray jSONArray, int i) throws JSONException {
        if (cls == String.class) {
            return jSONArray.getString(i);
        }
        if (cls == Boolean.class || cls == Boolean.TYPE) {
            return Boolean.valueOf(jSONArray.getBoolean(i));
        }
        if (cls == Double.class || cls == Double.TYPE) {
            return Double.valueOf(jSONArray.getDouble(i));
        }
        if (cls == Float.class || cls == Float.TYPE) {
            return Float.valueOf((float) jSONArray.getDouble(i));
        }
        if (cls == Integer.class || cls == Integer.TYPE) {
            return Integer.valueOf(jSONArray.getInt(i));
        }
        if (cls == Short.class || cls == Short.TYPE) {
            return Short.valueOf((short) jSONArray.getInt(i));
        }
        if (cls == Long.class || cls == Long.TYPE) {
            return Long.valueOf(jSONArray.getLong(i));
        }
        return jSONArray.get(i);
    }

    @NonNull
    private static String ˎ(@NonNull Field field) {
        String name;
        NamedField namedField = (NamedField) field.getAnnotation(NamedField.class);
        if (namedField != null) {
            name = namedField.name();
        } else {
            name = null;
        }
        if (name == null || name.isEmpty()) {
            return field.getName();
        }
        return name;
    }
}
